//
//  CreateGroupChatVC.m
//  AAIM
//
//  Created by awh on 2019/12/1.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "CreateGroupChatVC.h"
#import "ContactRequestLogic.h"
#import "ChatViewController.h"
#import "ConversationsViewController.h"
@interface CreateGroupChatVC ()<UITextFieldDelegate>
@property(nonatomic,strong)UILabel*groupName;
@property(nonatomic,strong)UITextField*groupNameTF;
@property(nonatomic,strong)UILabel*maxNumLbl;
@property(nonatomic,strong)UIView*lineV;
@property(nonatomic,strong)UILabel*currentNumLbl;
@property(nonatomic,strong)UILabel*searchLbl;
@property(nonatomic,strong)UILabel*searchTitle;
@property(nonatomic,strong)UIView*searchlineV;
@property(nonatomic,strong)UIButton*confirmButton;
@property(nonatomic,strong)UISwitch*switchBtn;

@property(nonatomic,copy)NSString*isSelect;
@end

@implementation CreateGroupChatVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle=@"创建群聊";
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.groupName];
    [self.view addSubview:self.groupNameTF];
    [self.view addSubview:self.lineV];
    [self.view addSubview:self.maxNumLbl];
    [self.view addSubview:self.currentNumLbl];
    [self.view addSubview:self.searchLbl];
    [self.view addSubview:self.searchTitle];
    [self.view addSubview:self.switchBtn];
    [self.view addSubview:self.searchlineV];
    [self.view addSubview:self.confirmButton];
    //是否开启搜索  默认不开启
    _isSelect=@"0";

}
//限制输入框的长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
    
    if (range.length + range.location > textField.text.length) {
        
        return NO;
        
    }
    
    NSUInteger length = textField.text.length + string.length - range.length;
    
    return length <= 30;
    
}

-(UILabel*)groupName
{
    if (!_groupName) {
        _groupName=[self CreateLabelWithFrame:CGRectMake(15, NavigationBarHeight+20, 150, 18) Font:15 titleColor:KHEXCOLOR(AllBackColor_titleColor)];
        _groupName.text=@"群名称";
    }
    return _groupName;
}
-(UITextField*)groupNameTF
{
    if (!_groupNameTF) {
        _groupNameTF=[[UITextField alloc]initWithFrame:CGRectMake(_groupName.x, _groupName.bottom+15, SCREEN_WIDTH-30, 35)];
        _groupNameTF.placeholder=@"请输入群名称";
        _groupNameTF.tintColor = KHEXCOLOR(@"333333");
        NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:_groupNameTF.placeholder attributes:@{NSForegroundColorAttributeName : KHEXCOLOR(@"#AFAFAF")}];
        _groupNameTF.attributedPlaceholder = placeholderString;
        _groupNameTF.textColor=KHEXCOLOR(@"#AFAFAF");
        _groupNameTF.font=[UIFont systemFontOfSize:15];
        _groupNameTF.delegate = self;
        _groupNameTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _groupNameTF.leftViewMode = UITextFieldViewModeAlways;
//        _groupNameTF.backgroundColor=[UIColor redColor];
    }
    return _groupNameTF;
}
-(UIView*)lineV
{
    if (!_lineV) {
        _lineV=[[UIView alloc]initWithFrame:CGRectMake(_groupNameTF.x, _groupNameTF.bottom+1, SCREEN_WIDTH-15, 0.5)];
        _lineV.backgroundColor=KHEXCOLOR(IM_Color_SeparateLine);
    }
    return _lineV;
}
-(UILabel*)maxNumLbl
{
    if (!_maxNumLbl) {
        _maxNumLbl=[self CreateLabelWithFrame:CGRectMake(_lineV.x, _lineV.bottom+10, 150, 13) Font:11 titleColor:KHEXCOLOR(@"#AFAFAF")];
        _maxNumLbl.text=@"普通群1000人";
    }
    return _maxNumLbl;
}
-(UILabel*)currentNumLbl
{
    if (!_currentNumLbl) {
        _currentNumLbl=[self CreateLabelWithFrame:CGRectMake(SCREEN_WIDTH-215, _lineV.bottom+10, 200, 13) Font:11 titleColor:KHEXCOLOR(@"#AFAFAF")];
        _currentNumLbl.textAlignment=2;
        _currentNumLbl.text=@"0/1000";
    }
    return _currentNumLbl;
}
-(UILabel*)searchLbl
{
    if (!_searchLbl) {
        _searchLbl=[self CreateLabelWithFrame:CGRectMake(15, _maxNumLbl.bottom+25, 150, 18) Font:15 titleColor:KHEXCOLOR(AllBackColor_titleColor)];
        _searchLbl.text=@"允许搜索该群";
    }
    return _searchLbl;
}

-(UILabel*)searchTitle
{
    if (!_searchTitle) {
        _searchTitle=[self CreateLabelWithFrame:CGRectMake(_searchLbl.x, _searchLbl.bottom+15, 200, 13) Font:11 titleColor:KHEXCOLOR(@"#AFAFAF")];
        _searchTitle.text=@"开启后，群可以被搜到";
    }
    return _searchTitle;
}
-(UISwitch*)switchBtn
{
    if (!_switchBtn) {
        _switchBtn=[[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-15-50, _searchLbl.y, 35, 20)];
        [_switchBtn addTarget:self action:@selector(ClickSwitchButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
    
}
-(UIView*)searchlineV
{
    if (!_searchlineV) {
        _searchlineV=[[UIView alloc]initWithFrame:CGRectMake(_groupNameTF.x, _searchTitle.bottom+10, SCREEN_WIDTH-15, 0.5)];
        _searchlineV.backgroundColor=KHEXCOLOR(IM_Color_SeparateLine);
    }
    return _searchlineV;
}

-(UIButton*)confirmButton
{
    if (!_confirmButton) {
        _confirmButton=[UIButton buttonWithType:UIButtonTypeCustom];
        _confirmButton.frame=CGRectMake(_searchlineV.x, _searchlineV.bottom+50, SCREEN_WIDTH-30, 40);
        _confirmButton.backgroundColor=KHEXCOLOR(AllBackColor_bule);
        _confirmButton.layer.masksToBounds=YES;
        _confirmButton.layer.cornerRadius = 5;
        [_confirmButton setTitle:@"创建" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmButton.titleLabel.font=[UIFont systemFontOfSize:15];
        [_confirmButton addTarget:self action:@selector(didButtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
    
}
-(void)ClickSwitchButton:(UISwitch*)sender
{
    if (sender.on) {
        NSLog(@"1111");
        _isSelect=@"1";
        
    }else
    {
        NSLog(@"2222");
        _isSelect=@"0";
    }
}
-(void)didButtnAction:(UIButton*)sender
{
    if ([NSObject ldy_isEmpty:_groupNameTF.text]) {
        [self showTipHudWithText:@"请填写群名称"];
        return;
    }
    sender.enabled = NO;
    [self showLoadingHud];
         [ContactRequestLogic requestCreateGroupWithGroupName:_groupNameTF.text isSelect:_isSelect Success:^(NSDictionary * _Nonnull Dict) {
                [self hideHud];
                //创建成功 保存本地数据 会话列表存储数据
                NSLog(@"创建成功的数据%@",Dict);
//                [[UserDataBaseTool shareUserDataBaseTool] reloadData:2 Success:^(BOOL isOK) {
//                    if (isOK) {
//                        [[NSNotificationCenter defaultCenter] postNotificationName:kMessageSystemCreateGroup object:nil];
//                    }
//                }];
        //        [self inviteFriendJoinGroup:Dict[@"data"][@"id"]];//把选择的人加入群聊
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                            // 新建将要push的控制器
                            ChatViewController *chatVC =[[ChatViewController alloc]init];
                            chatVC.chatType = ChatTypeGroup;
                             chatVC.hidesBottomBarWhenPushed = YES;
                            chatVC.gk_navTitle = Dict[@"data"][@"groupName"];
                            chatVC.receiverId = Dict[@"data"][@"id"];
                            // 获取当前路由的控制器数组
                            NSMutableArray *vcArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
                            // 打印当前路由的控制器数组
                            NSLog(@"1==the vcArray is %@", vcArray);
                            // 获取档期控制器在路由的位置
                            int index = (int)[vcArray indexOfObject:self];
//                            // 移除当前路由器
                            [vcArray removeObjectAtIndex:index];
                            NSLog(@"2==the vcArray is %@", vcArray);
                            // 添加新控制器

                               [vcArray addObject: chatVC];
                            
                            // 打印新增后的控制器数组
                            NSLog(@"3==the vcArray is %@", vcArray);
                            // 重新设置当前导航控制器的路由数组
                            [self.navigationController setViewControllers:vcArray animated:YES];
                   
                        
                });
            } failure:^(NSString * _Nonnull errMsg) {
                [self hideHud];
                [self showTipHudWithText:errMsg];
            }];

   
}

//- (void)inviteFriendJoinGroup:(NSString *)groupId{
//    NSString * fuuid;
//    NSString * fuuids;
//    for (ContactModel * model in _collectionDataArray) {
//        fuuid = [NSString stringWithFormat:@"%@",model.fuuid];
//        if ([CommonTools isEmpty:fuuids]) {
//            fuuids = [NSString stringWithFormat:@"%@",model.fuuid];
//        }else{
//            fuuids = [NSString stringWithFormat:@"%@,%@",fuuids,fuuid];
//        }
//    }
//    [ContactRequestLogic requestJoinGroupSetWithGroupId:groupId GroupCode:@"" fuuids:fuuids reason:@"" type:@"1" Success:^(NSDictionary * _Nonnull Dict) {
//        NSLog(@"邀请加入群聊%@",Dict);
//        NSLog(@"邀请信息%@",Dict[@"message"]);
//  
//    } failure:^(NSString * _Nonnull errMsg) {
//        NSLog(@"邀请加入群聊:%@",errMsg);
//    }];
//    
//}

-(UILabel*)CreateLabelWithFrame:(CGRect)Frame Font:(CGFloat)font titleColor:(UIColor*)titleColor
{
    UILabel * lbl=[[UILabel alloc]initWithFrame:Frame];
    lbl.font=[UIFont systemFontOfSize:font];
    lbl.textColor=titleColor;
    return lbl;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
